<?php
class ControllerExtensionModuleDownloadApp extends Controller {

    const DEFAULT_MODULE_SETTINGS = [
        'module_download_app_name' => '扫码下载APP',
        'module_download_app_status' => 0, /* Enabled by default*/
        'module_download_app_ios_url' => '',
        'module_download_app_android_url'=> '',
        'module_download_app_icon'=>'',
    ];

    const MODULE_NAME = 'module_download_app';

    private $error = array();

    public function index() {

        $this->load->model('setting/setting');
        $this->load->language('extension/module/download_app');
        $this->document->setTitle($this->language->get('heading_title'));

        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
            $this->model_setting_setting->editSetting(self::MODULE_NAME, $this->request->post);
            $this->session->data['success'] = $this->language->get('text_success');
            $this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module', true));
        }

        $data = array();
        $data['breadcrumbs'] = array();
        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
        );
        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_extension'),
            'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module', true)
        );
        $data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('extension/module/download_app', 'user_token=' . $this->session->data['user_token'], true)
        );

        $module_setting = $this->model_setting_setting->getSetting(self::MODULE_NAME);

        if (isset($this->request->post['module_download_app_name'])) {
            $data['module_download_app_name'] = $this->request->post['module_download_app_name'];
        } else {
            $data['module_download_app_name'] = $module_setting['module_download_app_name'];
        }
        if (isset($this->request->post['module_download_app_android_url'])) {
            $data['module_download_app_android_url'] = $this->request->post['module_download_app_android_url'];
        } else {
            $data['module_download_app_android_url'] = $module_setting['module_download_app_android_url'];
        }
        if (isset($this->request->post['module_download_app_ios_url'])) {
            $data['module_download_app_ios_url'] = $this->request->post['module_download_app_ios_url'];
        } else {
            $data['module_download_app_ios_url'] = $module_setting['module_download_app_ios_url'];
        }
        if (isset($this->request->post['module_download_app_icon'])) {
            $data['module_download_app_icon'] = $this->request->post['module_download_app_icon'];
        } else {
            $data['module_download_app_icon'] = $module_setting['module_download_app_icon'];
        }
        if (isset($this->request->post['module_download_app_status'])) {
            $data['module_download_app_status'] = $this->request->post['module_download_app_status'];
        } else {
            $data['module_download_app_status'] = $module_setting['module_download_app_status'];
        }

        $data['action']['cancel'] = $this->url->link('marketplace/extension', 'user_token='.$this->session->data['user_token'].'&type=module');
        $data['action']['save'] = "";
        $data['error'] = $this->error;
        $data['header'] = $this->load->controller('common/header');
        $data['column_left'] = $this->load->controller('common/column_left');
        $data['footer'] = $this->load->controller('common/footer');
        $htmlOutput = $this->load->view('extension/module/download_app', $data);
        $this->response->setOutput($htmlOutput);
    }

    /**
     * @return bool
     * 验证表单提交数据
     */
    public function validate() {
        if (!$this->user->hasPermission('modify', 'extension/module/download_app')) {
            $this->error['error_permission'] = true;
            return false;
        }

        if (!utf8_strlen($this->request->post['module_download_app_ios_url'])) {
            $this->error['module_download_app_ios_url'] = true;
        }

        if (!utf8_strlen($this->request->post['module_download_app_android_url'])) {
            $this->error['module_download_app_android_url'] = true;
        }

        if (!utf8_strlen($this->request->post['module_download_app_icon'])) {
            $this->error['module_download_app_icon'] = true;
        }

        return empty($this->error);
    }

    public function install(){
        $this->load->model('setting/setting');
        $this->model_setting_setting->editSetting(self::MODULE_NAME, self::DEFAULT_MODULE_SETTINGS);
    }

    public function uninstall(){
        $this->load->model('setting/setting');
        $this->model_setting_setting->deleteSetting(self::MODULE_NAME);
    }

}